﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using fbasync.web.Data;

namespace fbasync.web.Controls.TemplateEditor
{
    public partial class StoryTemplateItem : BaseUserControl
    {
        protected override void OnPreRender(EventArgs e)
        {
            InitControls();

            base.OnPreRender(e);
        }

        public string Title
        {
            get { return uiTitleBox.Text; }
            set { uiTitleBox.Text = value; }
        }

        public string Body
        {
            get { return uiBodyBox.Text; }
            set { uiBodyBox.Text = value; }
        }

        public bool BodyVisible
        {
            get { return uiBodyBox.Visible; }
            set 
            { 
                uiBodyBox.Visible = value;
                uiBodyTitle.Visible = value;
            }
        }

        public bool ActionDisabled { get; set; }
        public bool IsShortTemplate { get; set; }
        
        public int InternalId
        {
            get { return ((int?) ViewState["InternalId"]).GetValueOrDefault(Consts.UnknownInternalId); }
            set { ViewState["InternalId"] = value; }
        }

        public bool IsEditMode
        {
            get
            {
                StoryTemplateBundleItem bundleItem = GetParentBundleItem(this);

                return (bundleItem != null) && bundleItem.IsChanged;
            }
        }

        public string EditModeTextBoxCssClass
        {
            get
            {
                StoryTemplateBundleItem bundleItem = GetParentBundleItem(this);

                return null == bundleItem ? String.Empty : (IsShortTemplate ? bundleItem.EditModeShortTextBoxCssClass : bundleItem.EditModeTextBoxCssClass);
            }
        }

        public string ReadOnlyTextBoxCssClass
        {
            get
            {
                StoryTemplateBundleItem bundleItem = GetParentBundleItem(this);

                return null == bundleItem ? String.Empty : (IsShortTemplate ? bundleItem.ReadModeShortTextboxCssClass : bundleItem.ReadOnlyTextBoxCssClass);
            }
        }

        private StoryTemplateBundleItem GetParentBundleItem(Control control)
        {
            if (control == null) return null;
            if (control is StoryTemplateBundleItem) return (StoryTemplateBundleItem)control;

            return GetParentBundleItem(control.Parent);
        }

        private void InitControls()
        {
            bool isEditMode = IsEditMode;

            uiTitleBox.ReadOnly = !isEditMode;
            uiBodyBox.ReadOnly = !isEditMode;
            
            string cssClass = isEditMode ? EditModeTextBoxCssClass : ReadOnlyTextBoxCssClass;

            if (!String.IsNullOrEmpty(cssClass))
            {
                uiTitleBox.TextBoxCssClass = cssClass;
                uiBodyBox.TextBoxCssClass = cssClass;
            }

            uiActionCheck.Visible = !ActionDisabled && isEditMode;
        }

        public void AssignStoryTemplate(StoryTemplate template)
        {
            Title = null == template ? String.Empty : template.template_title;
            Body = null == template ? String.Empty : template.template_body;
        }

        internal void AssignStoryTemplate(TemplateEditorDataSet.ShortStoryTemplateRow shortStoryTemplateRow)
        {
            Title = shortStoryTemplateRow.Title;
            Body = shortStoryTemplateRow.IsBodyNull() ? String.Empty : shortStoryTemplateRow.Body;

            InternalId = shortStoryTemplateRow.UniqueId;
        }

        internal void AssignStoryTemplate(TemplateEditorDataSet.OneLineTemplateRow oneLineTemplateRow)
        {
            Title = oneLineTemplateRow.Title;
            BodyVisible = false;

            InternalId = oneLineTemplateRow.UniqueId;
        }

        internal void AssignOneLineControls(int bundleId)
        {
            AssignControls(TemplateEditorDataAccess.DataSet.OneLineTemplate.FindByBundleUniqueIdUniqueId(bundleId, InternalId));
        }

        internal void AssignShortStoryControls(int bundleId)
        {
            AssignControls(TemplateEditorDataAccess.DataSet.ShortStoryTemplate.FindByBundleUniqueIdUniqueId(bundleId, InternalId));
        }

        internal void AssignControls(TemplateEditorDataSet.ShortStoryTemplateRow shortStoryTemplateRow)
        {
            if (null != shortStoryTemplateRow)
            {
                shortStoryTemplateRow.Title = Title;
                shortStoryTemplateRow.Body = Body;
            }
        }

        internal void AssignControls(TemplateEditorDataSet.OneLineTemplateRow oneLineTemplateRow)
        {
            if (null != oneLineTemplateRow)
            {
                oneLineTemplateRow.Title = Title;
            }
        }

        internal void DeleteOneLineIfChecked(int bundleUniqueId)
        {
            if (uiActionCheck.Checked)
            {
                TemplateEditorDataAccess.DeleteOneLineStory(bundleUniqueId, InternalId);
            }
        }

        internal void DeleteShortStoryIfChecked(int bundleUniqueId)
        {
            if (uiActionCheck.Checked)
            {
                TemplateEditorDataAccess.DeleteShortStory(bundleUniqueId, InternalId);
            }
        }
    }
}